package com.eva.admin.api.system;

import com.eva.admin.biz.system.settings.dto.SaveSystemSettingDTO;
import com.eva.framework.cache.autowired.Cache;
import com.eva.framework.cache.autowired.CleanCache;
import com.eva.framework.common.api.BaseController;
import com.eva.admin.biz.system.settings.SystemSettingsBiz;
import com.eva.framework.common.constants.ResponseStatus;
import com.eva.framework.common.exception.BusinessException;
import com.eva.framework.rbac.authorize.ContainPermissions;
import com.eva.framework.common.model.ApiResponse;
import com.eva.framework.rbac.trace.Trace;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "系统设置")
@RestController
@RequestMapping("/system/settings")
public class SystemSettingsController extends BaseController {

    @Resource
    private SystemSettingsBiz systemSettingsBiz;

    @PostMapping("/theme/save")
    @CleanCache("theme_settings")
    @ApiOperation("保存主题设置")
    @ContainPermissions("system:settings:theme")
    public ApiResponse<?> save (@RequestBody SaveSystemSettingDTO dto) {
        // 验证设置码，避免拥有一个设置权限后可更改其它设置
        if (!"theme-settings".equals(dto.getCode())) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED);
        }
        systemSettingsBiz.save(dto);
        return ApiResponse.success(null);
    }

    @Trace(exclude = true)
    @Cache("theme_settings")
    @GetMapping("/value/{code}")
    @ApiOperation("根据编码查询")
    public ApiResponse<String> findValueByCode (@PathVariable String code) {
        return ApiResponse.success(systemSettingsBiz.findValueByCode(code));
    }
}
